package com.woniuxy01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 作者
 * @create 2025-11-07 14:30
 */
public class Test06 {
    public static void main(String[] args) {
        test04();
    }

    private static void test04() {
        List<String> strs = new ArrayList<>();
        strs.add("aaa");
        strs.add("bbb");
        strs.add("bc");
        strs.add("de");
        List<String> collect = strs.stream()
                .filter(str -> str.startsWith("b"))
                //将操作的结果收集成集合
//                .collect(Collectors.toList());
                .toList();
        System.out.println(collect);
    }

    private static void test03() {
        List<String> strs = new ArrayList<>();
        strs.add("aaa");
        strs.add("bbb");
        strs.add("bc");
        strs.add("de");
        strs.stream()
                .filter(str->str.startsWith("b"))
                .forEach(str-> System.out.println(str));

    }

    private static void test02() {
        List<String> strs = new ArrayList<>();
        strs.add("aba");
        strs.add("bbb");
        strs.add("bc");
        strs.add("de");
        //将集合转成stream流
        strs.stream()
                //调用filter,对流中的元素进行筛选过滤，
                //参数是一个Predicate断言型接口,使用test()方法定义的条件进行判断
                //满足条件的就保留，不满足就删除
                .filter(new Predicate<String>() {
                    @Override
                    public boolean test(String s) {
                        return s.length() >= 3;
                    }
                })
                //流中的每个元素调用Consumer接口的accept()方法
                .forEach(new Consumer<String>() {
                    @Override
                    public void accept(String s) {
                        System.out.println(s);
                    }
                });

        System.out.println(strs);
    }

    private static void test01() {
        //创建流
        List<String> strs1 = new ArrayList<>();
        Stream<String> stream1 = strs1.stream();

        String[] strs2 = {"aaa", "bbb", "ccc"};
        Stream<Object> stream = Arrays.stream(strs2);

        Stream<String> strs3 = Stream.of(strs2);
    }
}
